package com.andy.blog.dal.service.impl;

import com.andy.blog.common.constant.UserConstants;
import com.andy.blog.common.core.domain.entity.SysDictData;
import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.DateUtils;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.SysDictTypeDO;
import com.andy.blog.dal.mapper.SysDictDataMapper;
import com.andy.blog.dal.mapper.SysDictTypeMapper;
import com.andy.blog.dal.service.SysDictTypeDalService;
import com.andy.blog.dal.vo.DictTypeVO;
import com.andy.blog.dal.vo.SysDictTypeVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 字典
 * Created by andy on 2023-04-05 11:38
 */
@Service
public class SysDictTypeDalServiceImpl implements SysDictTypeDalService {

    @Resource
    private SysDictTypeMapper dictTypeMapper;

    @Resource
    private SysDictDataMapper dictDataMapper;

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<SysDictTypeDO> buildQueryWrapper(DictTypeVO dictTypeVO) {
        LambdaQueryWrapper<SysDictTypeDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(StringUtils.isNotBlank(dictTypeVO.getDictType()), SysDictTypeDO::getDictType, dictTypeVO.getDictType())
                .like(StringUtils.isNotBlank(dictTypeVO.getDictName()), SysDictTypeDO::getDictName, dictTypeVO.getDictName())
                .eq(StringUtils.isNotBlank(dictTypeVO.getStatus()), SysDictTypeDO::getStatus, dictTypeVO.getStatus())
                .ge(StringUtils.isNotBlank(dictTypeVO.getBeginTime()), SysDictTypeDO::getCreateTime, dictTypeVO.getBeginTime())
                .le(StringUtils.isNotBlank(dictTypeVO.getEndTime()), SysDictTypeDO::getCreateTime, dictTypeVO.getEndTime())
                .orderByDesc(SysDictTypeDO::getId);
        return lambdaQueryWrapper;
    }

    @Override
    public IPage<SysDictTypeDO> selectListByPage(DictTypeVO dictTypeVO) {

        LambdaQueryWrapper<SysDictTypeDO> lambdaQueryWrapper = buildQueryWrapper(dictTypeVO);

        IPage<SysDictTypeDO> page = new Page<>(dictTypeVO.getPageNum(), dictTypeVO.getPageSize());

        return dictTypeMapper.selectPage(page, lambdaQueryWrapper);
    }

    /**
     * 根据条件查询字典类型
     *
     * @param dictTypeVO 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictTypeDO> selectDictTypeList(DictTypeVO dictTypeVO) {
        LambdaQueryWrapper<SysDictTypeDO> lambdaQueryWrapper = buildQueryWrapper(dictTypeVO);
        return dictTypeMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 根据所有字典类型
     *
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictTypeDO> selectDictTypeAll() {
        return dictTypeMapper.selectList(new LambdaQueryWrapper<>());
    }



    /**
     * 根据字典类型ID查询信息
     *
     * @param id 字典类型ID
     * @return 字典类型
     */
    @Override
    public SysDictTypeDO selectDictTypeById(Long id) {
        return dictTypeMapper.selectById(id);
    }

    /**
     * 根据字典类型查询信息
     *
     * @param dictType 字典类型
     * @return 字典类型
     */
    @Override
    public SysDictTypeDO selectDictTypeByType(String dictType) {
        LambdaQueryWrapper<SysDictTypeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictTypeDO::getDictType, dictType);
        return dictTypeMapper.selectOne(queryWrapper);
    }

    /**
     * 新增保存字典类型信息
     */
    @Override
    public int insertDictType(SysDictTypeVO sysDictTypeVO) {

        SysDictTypeDO sysDictTypeDO = TransformUtils.transform(sysDictTypeVO, SysDictTypeDO.class);

        sysDictTypeDO.setCreateBy(SecurityUtils.getUsername());
        sysDictTypeDO.setCreateTime(DateUtils.getCurrentDate());

        try {
            return dictTypeMapper.insert(sysDictTypeDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }
    }

    /**
     * 修改保存字典类型信息
     *
     * @param sysDictTypeVO 字典类型信息
     * @return 结果
     */
    @Override
    public int updateDictType(SysDictTypeVO sysDictTypeVO) {
        SysDictTypeDO sysDictTypeDO = TransformUtils.transform(sysDictTypeVO, SysDictTypeDO.class);

        sysDictTypeDO.setUpdateBy(SecurityUtils.getUsername());
        sysDictTypeDO.setUpdateTime(DateUtils.getCurrentDate());

        try {
            return dictTypeMapper.updateById(sysDictTypeDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101, e);
        }
    }

    /**
     * 校验字典类型称是否唯一
     *
     * @param sysDictTypeVO 字典类型
     * @return 结果
     */
    @Override
    public boolean checkDictTypeUnique(SysDictTypeVO sysDictTypeVO) {
        LambdaQueryWrapper<SysDictTypeDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDictTypeDO::getDictType, sysDictTypeVO.getDictType()).last("limit 1");
        SysDictTypeDO sysDictTypeDO = dictTypeMapper.selectOne(lambdaQueryWrapper);
        if (Objects.nonNull(sysDictTypeDO) && !Objects.equals(sysDictTypeDO.getId(), sysDictTypeVO.getId())) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        return dictDataMapper.selectDictDataList(dictData);
    }

    @Override
    public int deleteByPrimaryKey(Long dictId) {
        try {
            return dictTypeMapper.deleteById(dictId);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000102, e);
        }
    }
}
